package shuati.chuanzhibei.chuanzhi24;

import java.util.*;

public class JumpingGame {
    // 用来存储格子得分
    private static int[] a;
    // 用来存储n个格子的跳跃方案
    private static boolean[] visited;
    private static int n;

    // 存储最佳得分
    private static int maxScore = 0;
    // 存储每个最佳得分对应的路径
    private static List<List<Integer>> bestPaths = new ArrayList<>();

    // 递归函数来计算得分
    private static void dfs(int jumpCount, int currentPos, int currentScore, List<Integer> path) {
        if (jumpCount == n) {
            // 当跳跃次数达到n，记录得分并更新最大得分
            if (currentScore > maxScore) {
                maxScore = currentScore;
                bestPaths.clear();
                bestPaths.add(new ArrayList<>(path));
            } else if (currentScore == maxScore) {
                bestPaths.add(new ArrayList<>(path));
            }
            return;
        }

        // 向左跳跃
        int leftPos = (currentPos - 1 + n) % n;
        if (!visited[leftPos]) {
            visited[leftPos] = true;
            path.add(leftPos + 1); // 记录跳跃路径 (加1是因为题目要求从1开始)
            dfs(jumpCount + 1, leftPos, currentScore + (jumpCount + 1) * a[leftPos], path);
            path.remove(path.size() - 1); // 回溯
            visited[leftPos] = false;
        }

        // 向右跳跃
        int rightPos = (currentPos + 1) % n;
        if (!visited[rightPos]) {
            visited[rightPos] = true;
            path.add(rightPos + 1); // 记录跳跃路径 (加1是因为题目要求从1开始)
            dfs(jumpCount + 1, rightPos, currentScore + (jumpCount + 1) * a[rightPos], path);
            path.remove(path.size() - 1); // 回溯
            visited[rightPos] = false;
        }
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        // 输入n和a数组
        n = sc.nextInt();
        a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
        }

        // 初始化visited数组，用于记录每个格子是否被访问过
        visited = new boolean[n];
        
        // 对每个格子作为起点进行深度优先搜索
        for (int start = 0; start < n; start++) {
            visited[start] = true;
            List<Integer> path = new ArrayList<>();
            path.add(start + 1); // 记录起点 (加1是因为题目要求从1开始)
            dfs(1, start, 0, path); // 从起点开始，第一次跳跃的得分为0
            visited[start] = false;
        }

        // 输出最大得分
        System.out.println(maxScore);

        // 输出所有最佳路径
        for (List<Integer> path : bestPaths) {
            for (int i = 0; i < path.size(); i++) {
                System.out.print(path.get(i) + " ");
            }
            System.out.println();
        }
    }
}
